package week_05;


import week_05.sort.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class SortMain {
    public static int[] generateIntArray(int length, int MAX_VALUE) {
        int[] arr = new int[length];
        for (int i = 0; i < length; i++) {
            arr[i] = (int) (Math.random() * MAX_VALUE);
        }
        return arr;
    }

    public static void printArray(int[] arr) {
        int i = 0;
        for (; i < arr.length - 1; i++) {
            System.out.print(arr[i] + ",");
        }
        System.out.println(arr[i]);
    }

    public static int[] copyArray(int[] arr) {
        int[] copy = Arrays.copyOf(arr, arr.length);
        return copy;
    }

    public static void main(String[] args) {
        int MAX_N = 10;
        int MAX_V = 5000;
        int k = 10;
        List<Sort> sorts = new ArrayList<Sort>() {{
            add(new BubbleSort());
            add(new SelectionSort());
            add(new InsertionSort());
            add(new HeapSort());
            add(new MergeSort());
            add(new QuickSort());
            add(new Quick2Sort());
        }};
        for (Sort sort : sorts) {
            System.out.println("__________________________________________________________________________________________________________");
            System.out.println(sort.toString() + ":start:");
            for (int i = 0; i < k; i++) {
                int length = (int) (MAX_N * Math.random());
                int MAX_VALUE = (int) (Math.random() * MAX_V);

                int[] arr = generateIntArray(length, MAX_VALUE);
                int[] copy = copyArray(arr);
                Arrays.sort(copy);
                sort.sort(arr);
                if (!isEqualArray(arr, copy)) {
                    System.out.println("JDK SORT:");
                    printArray(copy);
                    System.out.println("YOUR SORT:" + sort.toString());
                    printArray(arr);
                    throw new RuntimeException("sort error");
                }

            }
            System.out.println(sort.toString() + ": is success!");
        }


    }

    private static boolean isEqualArray(int[] arr, int[] copy) {
        if (arr.length != copy.length){
            return false;
        }
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] != copy[i]){
                return false;
            }
        }
        return true;
    }
}
